#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementByName(driver,u'管理',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
       btn = WaitforElementByName(driver,u'下载管理',30)
    except NoSuchElementException:
        driver.back()
        return
    else:
        btn.click()

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.yulong.android.coolmart:id/delete_icon",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            time.sleep(1)
            try:
                right_btn=WaitforElementById(driver,'com.yulong.android.coolmart:id/negativeButton',5)
            except NoSuchElementException:
                pass
            else:
                right_btn.click()
    driver.back()

def clickappdown(driver):
    try:
        btns=WaitforElementsById(driver,'com.yulong.android.coolmart:id/itemlayout',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        for btn in btns:
            try:
                size=WaitforElementById(btn,"com.yulong.android.coolmart:id/tv_count",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size.split('/')[1]
            if 'G' not in size:
                if 'K' in size or float(size.split('/')[1][0:-1])<20:
                    continue
            try:
                down_btn=WaitforElementById(btn,'com.yulong.android.coolmart:id/download_small_tv',10)
            except NoSuchElementException:
                continue
            else:
                down_btn.click()
                waittime=60
                while waittime:
                    try:
                        text=WaitforElementById(btn,'com.yulong.android.coolmart:id/download_size_tv',5).get_attribute("text")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0M/' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"
    #driver.back()



def soft(driver):
    categorys = [u'音乐电台',u'实用工具',u'社交通讯',u'视频直播',u'新闻阅读',u'时尚购物',u'投资理财',u'便捷生活',u'学习教育',u'母婴育儿',u'拍摄美化',u'运动健康',u'办公效率',u'旅游出行']
    try:
        WaitforElementByName(driver,u'应用',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def game(driver):
    categorys = [u'扑克棋牌',u'休闲益智',u'动作冒险',u'电子竞技',u'飞行射击',u'经营策略',u'体育竞速',u'角色扮演']
    try:
        WaitforElementByName(driver,u'游戏',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.yulong.android.coolmart'    # apk的包名
    desired_caps['appActivity'] = '.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #driver.find_element_by_name().tag_name
    del_appdown_task(driver)
    soft(driver)
    time.sleep(2)
    game(driver)
    driver.quit()

#run()